ITF.js ➔ ???   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
import { START_BIN, END_BIN, BINARIES } from './constants';
2
import Barcode from '../Barcode';
3
4
class ITF extends Barcode {
5
6
	valid() {
7
		return this.data.search(/^([0-9]{2})+$/) !== -1;
8
	}
9
10
	encode() {
11
		// Calculate all the digit pairs
12
		const encoded = this.data
13
			.match(/.{2}/g)
14
			.map(pair => this.encodePair(pair))
15
			.join('');
16
17
		return {
18
			data: START_BIN + encoded + END_BIN,
19
			text: this.text
20
		};
21
	}
22
23
	// Calculate the data of a number pair
24
	encodePair(pair) {
25
		const second = BINARIES[pair[1]];
26
27
		return BINARIES[pair[0]]
28
			.split('')
29
			.map((first, idx) => (
30
				(first === '1' ? '111' : '1') +
31
				(second[idx] === '1' ? '000' : '0')
32
			))
33
			.join('');
34
	}
35
}
36
37
export default ITF;
38